package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.ChoiceOppositePersonEntity;
import com.sgb.kjwl.view.ui.adapter.self.publish.OppositePersonAdpater;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

/**
 * 对接人筛选PopWindow
 */
public class OppositePersonPopWindow<T> extends BasePopWindow {
    private RecyclerView recyclerView;
    private TextView okView;
    private TextView resetView;
    private OppositePersonAdpater oppositePersonAdpater;
    private LinearLayout noDataLay;
    private EditText editText;
    private StoragePopWindow.InputTextInterface inputTextInterface;
    private Object choicedata;
    private ImageView imageDelete;

    public StoragePopWindow.ILodMore iLodMore;
    public OppositePersonPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_opposite;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.skill_list);
        okView = holder.getPop().findViewById(R.id.ok_view);
        editText = holder.getPop().findViewById(R.id.et_view);
        noDataLay = holder.getPop().findViewById(R.id.ll_empty);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        imageDelete = holder.getPop().findViewById(R.id.image_delete);


        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onResultClick != null) {
                    onResultClick.result(choicedata, null, null);
                }
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    List<ChoiceOppositePersonEntity> choiceOppositePersonEntityArrayList = new ArrayList<>();
                    ChoiceOppositePersonEntity storageManEntity = (ChoiceOppositePersonEntity) choicedata;//选择返回数据
                    List<ChoiceOppositePersonEntity> choiceOppositePersonEntityList = oppositePersonAdpater.getDatas();
                    for (ChoiceOppositePersonEntity personEntity:choiceOppositePersonEntityList){
                        if (choicedata==null){
                            personEntity.setChoice(false);
                        }else {
                            if (storageManEntity.getUserNo().equals(personEntity.getUserNo())&&storageManEntity.getname().equals(personEntity.getname())){
                                personEntity.setChoice(false);
                            }
                        }
                        choiceOppositePersonEntityArrayList.add(personEntity);
                    }
                    oppositePersonAdpater.setDatas(choiceOppositePersonEntityArrayList);
                    choicedata=null;
                    oppositePersonAdpater.notifyDataSetChanged();
                }catch (Exception e){
                    dismiss();
                }
               //dismiss();
            }
        });

        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    inputTextInterface.input(editText.getText().toString().trim());
                }
                return false;
            }
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

                if (!TextUtils.isEmpty(editText.getText().toString().trim())) {
                    imageDelete.setVisibility(View.VISIBLE);
                } else {
                    imageDelete.setVisibility(View.GONE);
                }
            }
        });

        imageDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editText.setText("");
                inputTextInterface.input("");
            }
        });
        oppositePersonAdpater = new OppositePersonAdpater(mContext, null);
        oppositePersonAdpater.setItemSelect(true);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(oppositePersonAdpater);
        oppositePersonAdpater.setOnItemClick(new AdapterOnItemClick() {
            @Override
            public void onItemClick(Object o, int position) {
                choicedata = o;
            }
        });
    }


    public void setData(List<T> list) {
        if (null != list && list.size() > 0) {
            oppositePersonAdpater.setDatas(list);
            recyclerView.setVisibility(View.VISIBLE);
            noDataLay.setVisibility(View.GONE);
        } else {
            recyclerView.setVisibility(View.GONE);
            noDataLay.setVisibility(View.VISIBLE);
        }
    }

    public interface InputTextInterface {
        void input(String s);
    }

    public void inputTextInterface(StoragePopWindow.InputTextInterface inputTextInterface) {
        this.inputTextInterface = inputTextInterface;
    }


    public interface ChoiceModel {

        public String getname();

        public boolean isChoice();

        public void setChoice(boolean choice);

    }

}
